package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.aspect.ExcelUtils;
import com.wy.model.Result;
import com.wy.model.Room;
import com.wy.service.RoomService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/12/216:25
 */
@Api("restful风格的api示例")
@RestController
@RequestMapping("room")
@Slf4j
public class RoomController {

    @Autowired
    private RoomService roomService;

    @ApiOperation(value = "查询列表")
    @GetMapping("list")
    public Result<Room> list1(HttpServletRequest request, Room room,
                              @RequestParam(defaultValue = "1") int pagenum,
                              @RequestParam(defaultValue = "2") int pagesize){
        log.info("进入列表查询方法");
        PageHelper.startPage(pagenum,pagesize);
        List<Room> roomList = roomService.list(room);
        PageInfo<Room> pageInfo = new PageInfo<>(roomList);
        return new Result(pageInfo.getTotal(),pageInfo.getList());
    }

    @ApiOperation(value = "查询单条")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "path")})
    @ApiResponses({@ApiResponse(code=400,message="id为空")})
    @GetMapping("getById/{id}")
    public Room getById(@PathVariable String id){
        return roomService.getById(id);
    }


    @ApiOperation(value = "新增修改")
    @PostMapping("save")
    public Boolean save(@RequestBody Room room){
        String id = room.getId();
        if(id !=null && !id.equals("")){
            roomService.update(room);
        } else{
            room.setId(UUID.randomUUID().toString());
            roomService.insert(room);
        }
        return true;
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public boolean delete(String ids){
        if(ids != null && !ids.equals("")){
            roomService.deletebth(ids.split(","));
        }
        return true;
    }

    @RequestMapping("export")
    public void export(HttpServletResponse response){
        List<Room> list = roomService.list(new Room());
        String[] columnNames = new String[]{"编码","状态","价格"};
        String[] columns = new String[]{"code","state","price"};
        ExcelUtils.exportExcel(response,list,columnNames,columns,"sheetname","filename");

        //创建工作薄：
      /*  HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = wb.createSheet("列表sheet");
        //创建表头
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("编码");
        row.createCell(1).setCellValue("状态");
        row.createCell(2).setCellValue("价格");

        for(int i=0;i<list.size();i++){
            Room room = list.get(i);
            HSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(room.getCode());
            row1.createCell(1).setCellValue(room.getState());
            row1.createCell(2).setCellValue(room.getPrice());
        }
        ServletOutputStream fileOutputStrem = null;
        try {
            //fileOutputStrem = new FileOutputStream(new File("D:\\wytest.xls"));  导出到默认磁盘
            response.setCharacterEncoding("UTF-8");
            response.reset();//清除缓存
            response.setContentType("octets/stream");
            response.addHeader("Content-Disposition", "attachment;filename=123.xls");
            fileOutputStrem = response.getOutputStream();
            wb.write(fileOutputStrem);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStrem.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }*/
    }

    @RequestMapping("importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file){
        //ExcelUtils.readexcell()
        //把file转成workbook
        /*try {
            InputStream inputStream = file.getInputStream();
            POIFSFileSystem fs = new POIFSFileSystem(inputStream);
            HSSFWorkbook workbook = new HSSFWorkbook(fs);//根据输入流获取工作薄
            //获取到工作薄的第一个sheet
            HSSFSheet sheet = workbook.getSheetAt(0);
            // 得到总行数
            int rowNum = sheet.getLastRowNum();
            for(int i=0;i<rowNum;i++){
                HSSFRow row = sheet.getRow(i + 1);
                Room room = new Room();
                room.setCode(row.getCell(0).getStringCellValue());
                room.setState(row.getCell(1).getStringCellValue());
                room.setPrice(row.getCell(2).getStringCellValue());
                //把对象插入数据库
                room.setId(UUID.randomUUID().toString());
                roomService.insert(room);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }
}
